package com.app.controller;

import com.app.model.ProcessFlowModel;
import com.app.service.ProcessFlowService;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.vo.ProcessFlowVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 办事流程
 */
@RestController
@Controller
@RequestMapping("/processFlow")
public class ProcessFlowController {
    @Autowired
    private ProcessFlowService processFlowService;
    // 查询列表
    @GetMapping("/getProcessFlowList")
    public Result getProcessFlowList(@RequestParam(defaultValue = "0") Integer page,
                                     @RequestParam(defaultValue = "10") Integer limit) {
        ProcessFlowVo processFlowList = processFlowService.getProcessFlowList(page, limit);
        return Result.success(processFlowList);
    }

    //详情
    @GetMapping("/getProcessFlowById")
    public Result getProcessFlowById(Integer id) {
        ProcessFlowModel processFlow = processFlowService.getById(id);
        return Result.success(processFlow);
    }
    //根据类型查
    @GetMapping("/getProcessFlowByType")
    public Result getProcessFlowByType(@RequestParam Integer page,
                                       @RequestParam Integer limit,Integer type) {
        ProcessFlowVo typeList = processFlowService.getByType(page,limit,type);
        return Result.success(typeList);
    }
    //新增和修改
    @PostMapping("/addAndUpdateProcessFlow")
    public Result addAndUpdateProcessFlow(@RequestBody ProcessFlowModel processFlowModel) {
        ProcessFlowModel processFlow = processFlowService.getById(processFlowModel.getId());
        if (processFlow != null) {
            processFlowModel.setUpdateTime(CommonHelp.getNowTimestamp());
            processFlowService.updateById(processFlowModel);
            return Result.success("");
        } else {
            processFlowModel.setCreateTime(CommonHelp.getNowTimestamp());
            processFlowService.save(processFlowModel);
            return Result.success("");
        }
    }
    //删除
    @GetMapping("/delProcessFlow")
    public Result delProcessFlow(Integer id) {
        processFlowService.removeById(id);
        return Result.success("");
    }
}
